package com.cross.xml;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.cross.core.AbstractReader;
import com.cross.core.Column;
import com.cross.core.Header;
import com.cross.core.Item;
import com.cross.core.Row;
import com.cross.util.StringUtils;

public class SimpleXmlReader extends AbstractReader {

	private SAXReader reader = new SAXReader();
	private Document document = null;
	private Element root = null;
	private Iterator<Element> iterator = null;

	private final String TYPE = "type";
	private final String PERCISION = "precision";
	private final String OBJECTNAME = "objectName";
	
	public SimpleXmlReader(File file) throws DocumentException {
		document = reader.read(file);
		init();
	}
	
	public SimpleXmlReader(InputStream is) throws DocumentException {
		document = reader.read(is);
		init();
	}
	
	public SimpleXmlReader(Reader r) throws DocumentException {
		document = reader.read(r);
		init();
	}
	
	private void init(){
		root = document.getRootElement(); // Get the root node of XML
		iterator = root.elementIterator();
		String objectName = null;
		if(iterator.hasNext()){
			Element dataList = iterator.next();
			objectName = dataList.attributeValue(OBJECTNAME).trim();
			iterator = dataList.elementIterator();
			if(iterator.hasNext()){
				header = parseHeader(iterator.next());
				header.setObjectName(objectName);
			}
		}
		if(header == null){
			throw new RuntimeException("没有数据结构信息");
		}
	}

	@Override
	protected boolean nextRow() {
		return nextRowImpl();
	}

	private boolean nextRowImpl() {
		if(!iterator.hasNext()){
			return false;
		}
		currentRow = toRow(iterator.next());
		return null != currentRow;
	}

	private Row toRow(Element e) {
		Row row = new Row();
		Iterator<Element> iterator = e.elementIterator();
		Element child = null;
		int number = 0;
		while (iterator.hasNext()) {
			child = iterator.next();
			row.append(toItem(child, header.getColumn(number)));
			number++;
		}
		return row;
	}
	
	private Header parseHeader(Element headerElement){
		Header header = new Header();
		Iterator<Element> columnsIterator = headerElement.elementIterator();
		Element child = null;
		int i=0;
		String precisionStr;
		int precision = -1;
		while (columnsIterator.hasNext()) {
			child = columnsIterator.next();
			precisionStr = child.attributeValue(PERCISION).trim();
			if(StringUtils.isNotBlank(precisionStr)){
				precision = Integer.parseInt(precisionStr);
			}
			header.add(new Column(i++, child.getName(), child.attributeValue(TYPE).trim(), precision));
		}
		return header;
	}

	private Item toItem(Element e, Column column) {
		Item item = new Item(e.getData(), column.getType(), column.getPrecision());
		return item;
	}

	@Override
	public void close() {
		
	}
}
